//
//  SWStageSummaryCell.swift
//  ChildStory
//
//  Created by linhan on 2016/12/14.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit

fileprivate let ContentInsets:UIEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 15)
fileprivate let TextContentInsets:UIEdgeInsets = UIEdgeInsetsMake(49, 14, 14, 14)
fileprivate let ContentFont:UIFont = UIFont.systemFont(ofSize: 14)

class SWStageSummaryCell: UITableViewCell
{
    weak var summary:SWKidEventSummary?
    
    private var _change:Bool = true
    private var _cellWidth:CGFloat = 0
    
    private var _featureTitleLabel:UILabel = UILabel()
    private var _countLabel:UILabel = UILabel()
    private var _contentLabel:UILabel = UILabel()
    private var _contentView:UIView = UIView()
    private var _iconView:UIImageView = UIImageView()
    
    init(style: UITableViewCellStyle, reuseIdentifier: String?, cellWidth:CGFloat)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        _cellWidth = cellWidth
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    class func getCellHeight(summary:SWKidEventSummary, cellWidth:CGFloat) -> CGFloat
    {
        let content:String = summary.content ?? ""
        let contentWidth:CGFloat = cellWidth - ContentInsets.left - ContentInsets.right - TextContentInsets.left - TextContentInsets.right
        let textHeight:CGFloat = StringUtil.getStringHeight(content, font: ContentFont, width: contentWidth)
        return TextContentInsets.top + textHeight + TextContentInsets.bottom
    }
    
    private func setup()
    {
        selectionStyle = .none
        backgroundColor = UIColor.clear
        contentView.backgroundColor = UIColor.clear
        
        let contentWidth:CGFloat = _cellWidth - ContentInsets.left - ContentInsets.right
        _contentView.frame = CGRectMake(ContentInsets.left, 0, contentWidth, 0)
        _contentView.backgroundColor = UIColor.white
        contentView.addSubview(_contentView)
        
        //类型图标
        _iconView.frame = CGRectMake(TextContentInsets.left, 13, 15, 15)
        _contentView.addSubview(_iconView)
        
        //数量
        _countLabel.font = UIFont.systemFont(ofSize: 12)
        _countLabel.textColor = UIColor(hex: 0xcdcdcd)
        _countLabel.textAlignment = .right
        _countLabel.text = "99"
        _countLabel.sizeToFit()
        _countLabel.frame = CGRectMake(_contentView.width - TextContentInsets.right - _countLabel.width, 15, _countLabel.width, 16)
        _countLabel.text = ""
        _contentView.addSubview(_countLabel)
        
        //数量
        let countIcon:UIImageView = UIImageView(image: UIImage(named:"event_cell_icon_count"))
        countIcon.frame = CGRectMake(_countLabel.x - countIcon.width - 3, 15, countIcon.width, countIcon.height)
        _contentView.addSubview(countIcon)
        
        let featureTitleLabelX:CGFloat = _iconView.right + 3
        _featureTitleLabel.font = UIFont.systemFont(ofSize: 12)
        _featureTitleLabel.textColor = UIColor(hex: 0xcdcdcd)
        _featureTitleLabel.frame = CGRectMake(featureTitleLabelX, 13, _countLabel.x - featureTitleLabelX, 16)
        _contentView.addSubview(_featureTitleLabel)
        
        let line = UIView()
        line.backgroundColor = UIColor(hex: 0xc9c9c9)
        line.frame = CGRectMake(TextContentInsets.left, _iconView.bottom + 4, _contentView.width - TextContentInsets.left - TextContentInsets.right, 1)
        _contentView.addSubview(line)
        
        _contentLabel.font = ContentFont
        _contentLabel.textColor = UIColor(hex: 0x6c6c6c)
        _contentLabel.numberOfLines = 0
        _contentLabel.frame = CGRectMake(TextContentInsets.left, TextContentInsets.top, line.width, 0)
        _contentView.addSubview(_contentLabel)
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let summary = summary , _change
        {
            _change = false
            let type:String = summary.type ?? "1"
            let features:[String] = summary.features?.flatMap({$0 as? String}) ?? []
            let featuresTitle:String = features.joined(separator: " ")
            _featureTitleLabel.text = featuresTitle
            _countLabel.text = "\(summary.count)"
            _iconView.image = UIImage(named:"event_cell_icon_type\(type)")
            
            let content:String = summary.content ?? ""
            let textHeight:CGFloat = StringUtil.getStringHeight(content, font: ContentFont, width: _contentLabel.width)
            _contentLabel.text = content
            _contentLabel.height = textHeight
            
            _contentView.height = _contentLabel.bottom + TextContentInsets.bottom
        }
    }
    
    
    
    
}
